package com.ctrip.demo.http;

import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class HttpUtils {

    public static void get(String url) throws IOException {
//        String url = "http://10.4.181.225:8080/artemis-discovery-service/api/discovery/lookup.json";
        RequestConfig.custom().setSocketTimeout(1000);
        //相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
        HttpClient httpClient = HttpClients.createDefault();
        //声明请求方式
        HttpGet httpGet = new HttpGet(url);
        //获取相应数据，这里可以获取相应的数据
        HttpResponse httpResponse = httpClient.execute(httpGet);
        //拿到实体
        HttpEntity httpEntity= httpResponse.getEntity();
        //获取结果，这里可以正对相应的数据精细字符集的转码
        String result = "";
        if (httpEntity != null) {
            result = EntityUtils.toString(httpEntity,"utf-8");

            System.out.println(result);
        }
        //关闭连接
        httpGet.releaseConnection();
    }

    public static void post(String url, String ... params) throws IOException {
        //需要传输的数据
        Map<String,Object> map = new HashMap<String, Object>();

        for (int i = 0; i < params.length; i++) {
            map.put(i + "", params[i]);
        }

        //谷歌的Gson
        Gson gson = new Gson();
        //相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
        HttpClient httpClient = HttpClients.createDefault();
        //声明请求方式
        HttpPost httpPost = new HttpPost(url);
        //设置消息头
        httpPost.setHeader("Content-Type","application/json;charset=utf-8");
        httpPost.setHeader("Accept","application/json");
        //设置发送数据(数据尽量为json),可以设置数据的发送时的字符集
        httpPost.setEntity(new StringEntity(gson.toJson(map),"utf-8"));
        //获取相应数据，这里可以获取相应的数据
        HttpResponse httpResponse = httpClient.execute(httpPost);
        //拿到实体
        HttpEntity httpEntity= httpResponse.getEntity();
        //获取结果，这里可以正对相应的数据精细字符集的转码
        String result = "";
        if (httpEntity != null) {
            result = EntityUtils.toString(httpEntity,"utf-8");

            System.out.println(result);
        }
        //关闭连接
        httpPost.releaseConnection();
    }

    public static void put(String url, String ... params) throws IOException {
        //需要传输的数据
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("1", params);
        map.put("2", params);
        //谷歌的Gson
        Gson gson = new Gson();
        //相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
        HttpClient httpClient = HttpClients.createDefault();
        //声明请求方式
        HttpPut httpPut = new HttpPut(url);
        //设置消息头
        httpPut.setHeader("Content-Type","application/json;charset=utf-8");
        httpPut.setHeader("Accept","application/json");
        //设置发送数据(数据尽量为json),可以设置数据的发送时的字符集
        httpPut.setEntity(new StringEntity(gson.toJson(map),"utf-8"));
        //获取相应数据，这里可以获取相应的数据
        HttpResponse httpResponse = httpClient.execute(httpPut);
        //拿到实体
        HttpEntity httpEntity= httpResponse.getEntity();
        //获取结果，这里可以正对相应的数据精细字符集的转码
        String result = "";
        if (httpEntity != null) {
            result = EntityUtils.toString(httpEntity,"utf-8");
            System.out.println(result);
        }
        //关闭连接
        httpPut.releaseConnection();
    }
    public static void delete(String url) throws IOException {
//相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
        HttpClient httpClient = HttpClients.createDefault();
        //声明请求方式
        HttpDelete httpDelete = new HttpDelete(url);
        //设置消息头(这里可以根据自己的接口来设定消息头)
        httpDelete.setHeader("Content-Type","application/json;charset=utf-8");
        httpDelete.setHeader("Accept","application/json");
        //获取相应数据，这里可以获取相应的数据
        HttpResponse httpResponse = httpClient.execute(httpDelete);
        //拿到实体
        HttpEntity httpEntity= httpResponse.getEntity();
        //获取结果，这里可以正对相应的数据精细字符集的转码
        String result = "";
        if (httpEntity != null) {
            result = EntityUtils.toString(httpEntity,"utf-8");
            System.out.println(result);
        }
        //关闭连接
        httpDelete.releaseConnection();
    }

}
